<template>
  <Form ref="formValidate"
        :model="formValidate"
        :rules="ruleValidate"
        :label-width="110"
        class="box">
    <FormItem label="彩票类型："
              prop="gender">
      <RadioGroup v-model="formValidate.gender"
                  @on-change="Radio_change">
        <Radio label="0"
               style="margin-right:50px">实体票</Radio>
        <Radio label="1">电子票</Radio>
      </RadioGroup>
    </FormItem>
    <FormItem>
      <Button type="primary"
              @click="handleSubmit('formValidate')"
              style="width:106px">确认</Button>
      <Button @click="handleReset('formValidate')"
              style="margin-left: 20px;width:106px">取消</Button>
    </FormItem>
  </Form>
</template>
<script>
import { getDicQuery, setDicSave } from '@/api/setting'
// import { routeEqual } from '@/libs/util'
export default {
  data () {
    return {
      formValidate: {
        gender: ''
      },
      ruleValidate: {
        gender: [
          { required: true, message: '彩票类型不能为空', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
  },
  created () {
  },
  mounted () {
    this.getTicketType()
    if (!this.hasPermission('moms:setting:tickettype')) {
      this.$router.replace({ name: 'error_401' })
    }
  },
  methods: {
    Radio_change (type) {
      if (type === '0') {
        this.formValidate.gender = '0'
      } else {
        this.formValidate.gender = '1'
      }
    },
    getTicketType () {
      getDicQuery({
        'shopId': this.$route.query.id,
        'dictKeys': 'outTicketType',
        'msType': '1',
        'merchantId': this.$store.state.user.merchantId
      }).then((res) => {
        if (res.data.status === 200) {
          this.formValidate.gender = res.data.data[0].outTicketType
        } else {
          this.$Message.error(`${res.data.error}`)
        }
      })
    },
    handleSubmit (name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          setDicSave({
            'shopId': this.$route.query.id,
            'msType': '1',
            'outTicketType': this.formValidate.gender,
            'merchantId': this.$store.state.user.merchantId
          }).then((res) => {
            if (res.data.status === 200) {
              this.$Message.success('设置成功')
              this.closeModal()
            } else {
              this.$Message.error(`${res.data.error}`)
            }
          })
        } else {
          this.$Message.error('彩票类型不能为空!')
        }
      })
    },
    closeModal () {
      this.$store.commit('closeTag', this.$route)
      // this.$store.commit('setTagNavList', this.$store.state.app.tagNavList.filter(item => !routeEqual(this.$route, item)))
      // this.$nextTick(() => {
      //   this.$router.push({
      //     name: 'store_setting', params: { id: this.$route.query.id }
      //   })
      // })
      if (this.$store.state.app.tagNavList[this.$store.state.app.tagNavList.length - 1].name !== 'store_setting') {
        this.$nextTick(() => {
          this.$router.push({
            name: 'store_setting', params: { id: this.$route.query.id }
          })
        })
      }
    },
    handleReset (name) {
      // this.$refs[name].resetFields()
      this.closeModal()
    }
  }
}
</script>
<style lang="less" scoped>
.box {
  width: 600px;
  margin: 0 auto;
  padding-top: 100px;
}
</style>
